<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>我的订单</title>
	<link rel="icon" href="http://static.xiaohong.work/img/favicon.ico">

	<!-- 引入样式 -->
	<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
	<link rel="stylesheet" type="text/css" href="http://static.xiaohong.work/css/all.css" />
	<link rel="stylesheet" type="text/css" href="http://static.xiaohong.work/css/pages-cart.css" />
</head>

<body>
<div id="app">
	<!--head-->
	<!-- 头部栏位 -->
	<!--页面顶部-->
	<div>
		<top/>
	</div>


	<div class="cart py-container">
		<!--All goods-->
		<div class="allgoods">
			<h4>全部商品
				<span></span>
			</h4>
			<div class="cart-main">
				<div class="yui3-g cart-th">
					<div class="yui3-u-1-4" style="width: 180px;margin-left: 20px">商品图片</div>
					<div class="yui3-u-1-4" style="width: 180px">商品名称</div>
					<div class="yui3-u-1-8">单价（元）</div>
					<div class="yui3-u-1-8">数量</div>
					<div class="yui3-u-1-8" style="width: 182px">总计（元）</div>
					<div class="yui3-u-1-8" style="width: 160px">支付状态</div>
					<div class="yui3-u-1-8" style="width: 90px">操作</div>
				</div>
				<div class="cart-item-list">
					<div class="cart-body">
						<!-- vue loadlist -->
						<div class="cart-list" v-for="(item,index) in orderItems" :key="index">
							<ul class="goods-list yui3-g">
								<li class="yui3-u-6-24">
									<div class="good-item">
										<div class="item-img">
											<img :src="item.image" style="width: 100px;height: 90px" />
										</div>
									</div>
								</li>
								<li class="yui3-u-5-24">
									<div class="name" v-text="item.name" style="width: 250px"></div>
								</li>
								<li class="yui3-u-1-8">
									<span class="price" v-text="item.price" style="width: 200px"></span>
								</li>
								<li class="yui3-u-1-8">
									<span class="num" v-text="item.num" style="width: 200px"></span>
								</li>
								<li class="yui3-u-1-8">
									<span class="price" v-text="item.payMoney" style="width: 200px"></span>
								</li>
								<li class="yui3-u-1-8" style="width: 160px">
									<span v-if="item.status === 0" class="price" style="background-color:#a9dba9;width: 160px">待付款</span>
									<span v-else-if="item.status === 1" class="price" style="background-color:greenyellow;width: 160px">已付款</span>
									<span v-else-if="item.status === 2" class="price" style="background-color:red;width: 160px">付款失败</span>
									<span v-else-if="item.status === 3" class="price" style="background-color:indianred;width: 160px">已退款</span>
									<span v-else-if="item.status === 4" class="price" style="background-color:orangered;width: 160px">退款失败</span>
									<span v-else="item.status === 5" class="price" style="background-color:grey;width: 160px">交易关闭</span>
								</li>
								<li class="yui3-u-1-8" style="width: 50px">
									<span @click="refund(item.orderId,item.id)" v-show="item.status === 1"><a href="#" style="text-decoration: none">退款</a></span><br>
									<span @click="query(item.orderId,item.id)"><a href="#" style="text-decoration: none">刷新状态</a></span><br>
									<span @click="pay(item.orderId)" v-show="item.status === 0"><a href="#" style="text-decoration: none">付款</a></span>
								</li>
							</ul>
						</div>

					</div>
				</div>
			</div>
			<div class="clearfix"></div>
			<div class="liked">
				<ul class="sui-nav nav-tabs">
					<li class="active">
						<a href="#index" data-toggle="tab">猜你喜欢</a>
					</li>
					<li>
						<a href="#profile" data-toggle="tab">特惠换购</a>
					</li>
				</ul>
				<div class="clearfix"></div>
				<div class="tab-content">
					<div id="index" class="tab-pane active">
						<div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
							<div class="carousel-inner">
								<div class="active item">
									<ul>
										<li>
											<img src="http://static.xiaohong.work/img/like1.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like2.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like3.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like4.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
									</ul>
								</div>
								<div class="item">
									<ul>
										<li>
											<img src="http://static.xiaohong.work/img/like1.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like2.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like3.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
										<li>
											<img src="http://static.xiaohong.work/img/like4.png" />
											<div class="intro">
												<i>Apple苹果iPhone 6s (A1699)</i>
											</div>
											<div class="money">
												<span>$29.00</span>
											</div>
											<div class="incar">
												<a href="#" class="sui-btn btn-bordered btn-xlarge btn-default">
													<i class="car"></i>
													<span class="cartxt">加入购物车</span>
												</a>
											</div>
										</li>
									</ul>
								</div>
							</div>
							<a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
							<a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
						</div>
					</div>
					<div id="profile" class="tab-pane">
						<p>特惠选购</p>
					</div>
				</div>
			</div>
		</div>
	</div>
	<!-- 底部栏位 -->
	<!--页面底部-->
	<div>
		<end/>
	</div>
	<!--页面底部END-->
</div>
<script type="text/javascript" src="http://static.xiaohong.work/js/plugins/jquery/jquery-3.5.1.min.js"></script>
<!-- 引入组件库 -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.8/dist/vue.js"></script>
<script src="http://static.xiaohong.work/js/axios.min.js"></script>
<script src="http://static.xiaohong.work/js/common.js"></script>


<script>
	new Vue({
		el: '#app',
		data() {
			return {
				orderItems: [],
				user: JSON.parse(window.localStorage.getItem('user'))
			}
		},
		created: function () {
			this.getOrderItem();
		},
		methods: {
			order(){
				//判断勾选商品是否为空
				if (xiaohong.store.get("Cars").length === 0){
					alert("请勾选需要购买的商品！")
				}else {
					window.location = 'http://www.xiaohong.work/order.html';
				}
			},
			// 获取订单
			async getOrderItem() {
				await xiaohong.http.get('/orderItem/self').then(async resp => {
					if(resp.data.code === 20000){
						this.orderItems = resp.data.data;
					}else {
						alert(resp.data.message);
					}
				}).catch(() => alert('获取订单失败'));
			},
			// 付款
			async pay(sn){
				// 发起支付
				window.location = 'http://gateway.xiaohong.work/api/pay/zfbPay/pay?sn='
						+sn+"&token="+window.localStorage.getItem("accessToken");
			},
			// 刷新订单状态
			async query(sn,id) {
				await xiaohong.http.get('/pay/aliPay/query/'+sn+'/'+id).then(async resp => {
					// 刷新订单
					// this.getOrderItem();
					if(resp.data.code === 20000){
						// 刷新订单
						// this.orderItems.forEach(orderItem => {
						// 	if (orderItem.id === resp.data.data.id){
						// 		orderItem.status = resp.data.data.status;
						// 	}
						// });
						this.getOrderItem();
					}else {
						alert(resp.data.message);
					}
				}).catch(() => alert('刷新订单状态失败'));
			},
			// 退款
			async refund(orderId,OrderItemId){
				await xiaohong.http.put('/pay/aliPay/refund/'+OrderItemId).then(async resp => {
					if(resp.data.code === 20000){
						// 刷新订单
						this.query(orderId,OrderItemId);
					}else {
						alert(resp.data.message);
					}
				}).catch(() => alert('退款状态失败'));
			}

		},
		watch:{
			allCheck(newVal){
				if (newVal){
					this.choiceList = this.shopcarlist;
					xiaohong.store.set("Cars",this.choiceList);
					this.sum = 0;
					this.paySum = 0;
					for (let i = 0; i <this.choiceList.length; i++) {
						this.sum += this.choiceList[i].money;
						this.paySum += this.choiceList[i].payMoney;
					}
				}else{
					if(this.unCheck_flag){
						this.choiceList = [];
						xiaohong.store.set("Cars",this.choiceList);
						this.sum = 0;
						this.paySum = 0;
					}else {
						this.sum = 0;
						this.paySum = 0;
						xiaohong.store.set("Cars",this.choiceList);
						for (let i = 0; i <this.choiceList.length; i++) {
							this.sum += this.choiceList[i].money;
							this.paySum += this.choiceList[i].payMoney;
						}
					}
				}

			},
			choiceList(newVal){
				// newVal === this.shopcarlist 好像有bug,即使一样了也没反应。。。
				if (newVal.length !== this.shopcarlist.length){
					this.unCheck_flag = false;
					this.allCheck = false;
				}else {
					this.unCheck_flag = true;
					this.allCheck = true;
				}
				this.sum = 0;
				this.paySum = 0;
				xiaohong.store.set("Cars",this.choiceList);
				for (let i = 0; i <this.choiceList.length; i++) {
					this.sum += this.choiceList[i].money;
					this.paySum += this.choiceList[i].payMoney;
				}

			}
		},
		components: {
			top: () => import("http://static.xiaohong.work/js/pages/top.js"),
			end: () => import("http://static.xiaohong.work/js/pages/end.js")
		}
	})
</script>

</body>

</html>
